﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cit.CDE.Net.Module.Commons;
using Module;
using Cit.CDE.Net.Module;
using cde.SystemCommon;

using UI.UserControl;
using Cit.CDE.Net.Service.ModuleFactory;
using System.Web.UI.HtmlControls;

public partial class main_main_SearchResult2 : UserControl.CommonPage
{
    /// <summary>
    /// EntityManager
    /// </summary>
    private CDEEntityManager em;

    private String EM;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        Master.strHc = "1";
        //设置母版页控件显示
        ((Literal)Master.FindControl("lblpath")).Text = MenuPath;

        EM = Request.QueryString[WEBConstants.EM_QUERY_STRING];

        //初始化EM
        if (!Cit.CDE.Net.Module.Commons.CDEConstants.IsEmpty(EM))
        {
            em = cde.cache.CDEFactory.getEMByFullName(EM);
        }
        else
        {
            em = null;
        }

        Master.sc = this.CreatSc(em);
        //设置母版页控件事件
        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).ItemDataBound += new DataGridItemEventHandler(this.itemDataBound);

    }
     /// <summary>
    /// 操作事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void itemDataBound(object sender, DataGridItemEventArgs e)
    {
        //控制母版页控件显示内容
        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).Columns[((SimpleResultDataGrid)Master.FindControl("dgmeisai")).Columns.Count - 1].Visible = true;
    }
    /// <summary>
    /// 删除操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cmdDel_Click(object sender, EventArgs e)
    {

        string strErr = string.Empty;
        string strMsg = string.Empty;
        List<CDEEntity> S_listEntity = (List<CDEEntity>)Session[WEBConstants.LIST_SESSION_ID + EM];
        List<CDEEntity> listEntity = new List<CDEEntity>();
        foreach (int index in Master.listIndex)
        {
            S_listEntity[index].Operation = CDEDataOperation.DELETE;
            listEntity.Add(S_listEntity[index]);
        }
        CDEService.UpdateEntities(em, listEntity);
        foreach (int index in Master.listIndex)
        {
            S_listEntity.Remove(S_listEntity[index]);
        }
        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).ListEntity = S_listEntity;
        
        Session["listEntity"] = S_listEntity;

        Master.GetFirstNum();

        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).DataBind();

        for (int index = 0; index < listEntity.Count; index++)
        {
            if (listEntity[index].Result == CDEDataOperationResult.ERROR)
            {
                strErr = strErr + (((main_main_MasterPage)this.Master).listIndex[index] + 1).ToString() + ",";
            }
        }
        if (strErr != string.Empty)
        {
            strErr = strErr.Substring(0, strErr.Length - 1);
            strMsg = Resources.ResourceErrMsg.DELETE_ERR;
            strMsg = strMsg.Replace("{0}", strErr);
            showMessageErrAndOk(strMsg);
        }
        else
        {
            //系统日志
            //SysLog.AddSysLog(listEntity, this.GetLoginOprator());

            showMessageErrAndOk(Resources.ResourceAdmin.MSG_ROLE_DEL_RESULT_OK);
        }
    }
        /// <summary>
    /// 会员卡信息管理操作处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void StructLinkBtn_Click(object sender, EventArgs e)
    {
        LoginOprator op = this.GetLoginOprator();

        LinkButton btn = (LinkButton)sender;

        string operate = btn.ID.ToString();

        CDEEntity entity = ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).ListEntity[((DataGridItem)btn.Parent.Parent.Parent).ItemIndex];
        string id = ((HtmlInputCheckBox)((DataGridItem)btn.Parent.Parent.Parent).Cells[0].FindControl("chkMeisai")).Value;


        //挂失
        if (operate.Equals("reportTheLoss"))
        {
            this.changeStatus(id, "2", "挂失");

        }
    }

    /// <summary>
    /// 修改会员状态
    /// </summary>
    /// <param name="id"></param>
    /// <param name="status"></param>
    /// <param name="op"></param>
    protected void changeStatus(string id, string status, string op)
    {
        List<CDEEntity> listEntity = (List<CDEEntity>)Session[WEBConstants.LIST_SESSION_ID + EM];

        string cardId = "";

        foreach (CDEEntity ent in listEntity)
        {

            if (ent._Id.Equals(id))
            {
                ent.Operation = CDEDataOperation.UPDATE;

                ent.set("B_CD_STATUS", status);

                cardId = ent.get("A_CARD_NO").ToString();

            }
        }
        CDEEntityManager userEm = CDEEMFactory.getEMByFullName("WEB.STATUS_USERCARD");
        CDESearchCriteria userSc = new CDESearchCriteria(userEm);
        userSc.addCondition("CARD_NO", cardId, CDEConstCompare.Equality);

        List<CDEEntity> userlistEntity = CDEService.LoadEntities(userEm, userSc);
        //作废
        if (status.Equals("3"))
        {   //修改会员信息
            foreach (CDEEntity en in userlistEntity)
            {
                en.Operation = CDEDataOperation.UPDATE;
                en.set("CD_STATUS", "");
                en.set("CARD_NO", "");
                en.set("USER_PASSWORD", "");
                en.set("CD_REGDATE", "");
            }
            CDEService.UpdateEntities(userEm, userlistEntity);
            //修改会员消费信息
            CDEEntityManager spendEm = CDEEMFactory.getEMByFullName("WEB.INVOICE");
            CDESearchCriteria spendSc = new CDESearchCriteria(spendEm);
            spendSc.addCondition("CARD_NO", cardId, CDEConstCompare.Equality);
            List<CDEEntity> spendList = CDEService.LoadEntities(spendEm, spendSc);
            if (spendList.Count > 0)
            {

                foreach (CDEEntity spendEN in spendList)
                {
                    spendEN.Operation = CDEDataOperation.UPDATE;
                    spendEN.set("CARD_NO", "");
                }
                CDEService.UpdateEntities(spendEm, spendList);

                //SysLog.AddSysLog(spendList, this.GetLoginOprator());
            }
            //修改会员卡信息
            CDEEntityManager cardEm = CDEEMFactory.getEMByFullName("WEB.CARD_RAW_DATA");
            CDESearchCriteria cardSc = new CDESearchCriteria(cardEm);
            cardSc.addCondition("CARD_NO", cardId, CDEConstCompare.Equality);
            List<CDEEntity> cardList = CDEService.LoadEntities(cardEm, cardSc);

            foreach (CDEEntity cardEN in cardList)
            {
                cardEN.Operation = CDEDataOperation.UPDATE;
                cardEN.set("CREATE_TIME", "");
                cardEN.set("STATUS", "0");
            }
            CDEService.UpdateEntities(cardEm, cardList);

           // SysLog.AddSysLog(cardList, this.GetLoginOprator());

            CDESearchCriteria newSc = new CDESearchCriteria(em);
            newSc.addCondition("A_CARD_NO", cardId, CDEConstCompare.Equality);
            List<CDEEntity> newList = CDEService.LoadEntities(em, newSc);




            for (int i = 0; i < listEntity.Count; i++)
            {

                if ((listEntity[i]).Id.Equals(id))
                {
                    listEntity[i] = newList[0];
                }
            }

        }
        else
        {

            foreach (CDEEntity en in userlistEntity)
            {
                en.Operation = CDEDataOperation.UPDATE;
                en.set("CD_STATUS", status);
            }
            CDEService.UpdateEntities(userEm, userlistEntity);
        }

        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).ListEntity = listEntity;
        Master.GetFirstNum();
        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).DataBind();
        Session["listEntity"] = listEntity;

        if (userlistEntity[0].Result == CDEDataOperationResult.ERROR)
        {

            base.showMessageErrAndOk(op + "失败");
        }
        else
        {

            base.showMessageErrAndOk(op + "成功");
            //系统日志
           // SysLog.AddSysLog(userlistEntity, this.GetLoginOprator());
        }
    }
}
